const carDao=require('../dao/carDao');

//从购物车查找物品
async function findByAll() {
    try{
        var data = await carDao.findByAll();
        return {success:true,things:data};
    }catch(e){
        return {success:false,msg:'database is error!'};
    }
}
async function findByCar(imgsrc) {
    try{
        var data = await carDao.findByCar(imgsrc);
        return {success:true,things:data};
    }catch(e){
        return {success:false,msg:'database is error!'};
    }
}
//从购物车增加物品
async function addThing(thing) {
    try{
        //从物品数据库获取物品信息
        // console.log('service',thing);
        var data = await carDao.addThing(thing);
        return {success:true,things:data};
    }catch(e){
        //不能返回
        return {success:false,msg:'database is error!'};
    }
}
//从购物车删除物品
async function deleteThing(imgsrc) {
    try{
        console.log('service',imgsrc);
        var data = await carDao.findByCar(imgsrc);
        if(data!=null||data!=undefined||data!=''){
            var data = await carDao.deleteThing(imgsrc);
            return {success:true,things:data};
        }
    }catch(e){
        //不能返回
        return {success:false,msg:'database is error!'};
    }
}
//从购物车修改物品数量
async function modifyThing(imgsrc,number) {
    try{
        var data = await carDao.modifyThing(imgsrc,number);
        return {success:true,things:data};
    }catch(e){
        //不能返回
        return {success:false,msg:'database is error!'};
    }
}
exports.addThing=addThing;
exports.findByCar=findByCar;
exports.findByAll=findByAll;
exports.deleteThing=deleteThing;
exports.modifyThing=modifyThing;